package bb.lanxing.adapter.discover;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.fragment.discover.PlanListFragment;
import bb.lanxing.model.database.BbsType;

public class PlanTabAdapter extends FragmentPagerAdapter {
    private List<BbsType> tabLayoutList;
    private List<Fragment> fragmentList = new ArrayList<>();

    public PlanTabAdapter(FragmentManager fm, List<BbsType> bbsTypeList) {
        super(fm);
        tabLayoutList = bbsTypeList;
        for (BbsType bbsType : tabLayoutList) {
            PlanListFragment planListFragment = new PlanListFragment();
            planListFragment.setType(bbsType.typeId);
            fragmentList.add(planListFragment);
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabLayoutList.get(position).typeName;
    }

    public void updateData(List<BbsType> dataList) {
        tabLayoutList = dataList;
        for (BbsType bbsType : dataList) {
            PlanListFragment planListFragment = new PlanListFragment();
            planListFragment.setType(bbsType.typeId);
            fragmentList.add(planListFragment);
        }
        notifyDataSetChanged();
    }
}